#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
};

class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) 
    {
        vector<vector<int>> vv;
        queue<TreeNode*> q;
        if (root == nullptr)
        {
            return vv;
        }
        int size = 1;
        q.push(root);
        while (size)
        {
            vector<int> v;
            while (size--)
            {
                TreeNode *front = q.front();
                q.pop();
                v.push_back(front->val);
                
                if (front->left)
                {
                    q.push(front->left);
                }
                if (front->right)
                {
                    q.push(front->right);
                }
            }
            vv.push_back(v);
            size = q.size();
        }
        reverse(vv.begin(), vv.end());
        return vv;
    }
};